package com.example.maze;

import tw.fc.agui.*;

import android.content.Context;
import android.view.*;

public class GamePanel extends GraphicPanel 
                       implements MazeContentListenerI,
                                  RadarManagerListenerI{
   
   //[ 最上排的系統列的高
   private final int       SYS_BAR_SIZE=DataSet.sysBarHeight;
   private       Cell[][]  maze        = DataSet.MAZE_CELLS;
   private       int       screenWidth, 
                           screenHeight;
   private boolean isShowRadar= false;
   private Radar   mRadar     = new Radar();
   //[ ---------- construct ----------
   public GamePanel(Context context) {
      super(context);
      
      screenWidth = getWidth();
      screenHeight= getHeight()-SYS_BAR_SIZE;
      //[ MazeContentListenerI
      DataSet.addMCListener(this);
      //[ ShowRadarListenerI
      DataSet.addSRListener(this);
      this.addObj(new Dpad(screenWidth, screenHeight));
      invalidate();
   }
   //] ---------- construct ----------
   
   public void updateCellsOrgXY(){
    //[ 重設這些 Cell各自的繪圖起始點
      for(int i=DataSet.fromM, pi=0; i<=DataSet.toM; i++, pi++) {
         for(int j=DataSet.fromN, pj=0; j<=DataSet.toN; j++, pj++) {
            maze[i][j].setDrawOrg(pi, pj);
         }
      }
   }
   @Override 
   public boolean onDown(MotionEvent e) {
      super.onDown(e);
      return true;
   }

   @Override
   public void onDraw(GrCanvas g) {
      g.setAntiAlias(true); 
      updateCellsOrgXY();
      for(int i=DataSet.fromM; i<=DataSet.toM; i++) {
         for(int j=DataSet.fromN; j<=DataSet.toN; j++) {
            maze[i][j].paintTo(g);
         }
      }
      this.paintAllObj(g);
      if(isShowRadar) mRadar.paintTo(g);
   }
   
   @Override public void positionChanged(){ invalidate(); }
   @Override public void markChanged()    { invalidate(); }
   @Override public void showRadar()      { 
      isShowRadar= true;
      invalidate(); 
   }
   @Override public void hideRadar()      { 
      isShowRadar= false;
      invalidate(); 
   }
}
